package domain;

import java.util.ArrayList;
import java.util.List;

import domain.enums.GameStatus;
import domain.enums.PlayerType;

public class Board 
{
	public final int ROWS = 8;
	public final int COLS = 8;
	
	public ArrayList<Player> blackPlayers;
	public ArrayList<Player> whitePlayers;
	
	//copy constructor
	public Board(Board other)
	{
		blackPlayers = new ArrayList<Player>();
		whitePlayers = new ArrayList<Player>();
		
		for (Player p : other.blackPlayers)
		{
			blackPlayers.add((Player)p.clone());
		}
		for (Player p : other.whitePlayers)
		{
			whitePlayers.add((Player)p.clone());
		}
	}
	
	public Board()
	{
		blackPlayers = new ArrayList<Player>();
		whitePlayers = new ArrayList<Player>();
	}
	
	public String tileOccupied(int x, int y)
	{
		Position tile = new Position(x, y);
		
		for(Player black : blackPlayers)
		{
			if(black.getPosition().equals(tile))
				return "B";
		}
		
		for(Player white : whitePlayers)
		{
			if(white.getPosition().equals(tile))
				return "W";
		}
		
		return "_";
	}
	
	public List<Position> getAvailableMoves()
	{
		ArrayList<Position> a_positions = new ArrayList<Position>();
		
		List<Position> allTakenPosition = getBPositions();
		allTakenPosition.addAll(getWPositions());
		
		//row
		for(int y=0; y<8; y++)
		{
			//column
			for(int x=0; x<8; x++)
			{
				Position currentPosition = new Position(x,  y);
				if (!allTakenPosition.contains(currentPosition)) //position not taken
				{
					a_positions.add(currentPosition);
					break;
				}
			}
		}
		
		//row
		for(int y=0; y<8; y++)
		{
			//column
			for(int x=7; x>-1; x--)
			{
				Position currentPosition = new Position(x,  y);
				if (!allTakenPosition.contains(currentPosition))
				{
					if(!a_positions.contains(currentPosition))
						a_positions.add(currentPosition);
					break;
				}
			}
		}
		
		return a_positions;
	}
	
	//get all black's positions
	public List<Position> getBPositions()
	{
		ArrayList<Position> b_pos = new ArrayList<Position>();
		
		for(Player p : blackPlayers)
		{
			b_pos.add(p.getPosition());
		}
		
		return b_pos;
	}
	
	//get all white's positions
	public List<Position> getWPositions()
	{
		ArrayList<Position> w_pos = new ArrayList<Position>();
	
		for(Player p : whitePlayers)
		{
			w_pos.add(p.getPosition());
		}
		
		return w_pos;
	}
	
	public GameStatus getGameStatus()
	{
	
		for(int y=0; y<8; y++)
		{
			int consecutiveCounter = 0;
			String consecutiveType = "";
			
			for(int x=0; x<8; x++)
			{
				String currentType = tileOccupied(x, y);
				if(x == 0)
				{
					consecutiveCounter = 1;
					consecutiveType = currentType;
				}
				else if(currentType.equals(consecutiveType))
				{
					consecutiveCounter++;
				}
				else
				{
					if(x >= 4)
						break;
					
					consecutiveType = currentType;
					consecutiveCounter = 1;
				}
				
				if(consecutiveCounter >= 5)
				{
					if(consecutiveType == "B")
						return GameStatus.B_WINS;
					else if(consecutiveType == "W")
						return GameStatus.W_WINS;
				}
			}
		}
		
		return GameStatus.NO_WINNER;
	}
	
	public void PerformMove(Move move)
	{
		if(move.getIsBlack())
		{
			blackPlayers.add(new Player(move.getPosition(), PlayerType.BLACK));
		}
		else
		{
			whitePlayers.add(new Player(move.getPosition(), PlayerType.WHITE));
		}
	}
	
}
